package com.solin.blog.web.controller;

import com.solin.blog.service.BlogTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author solin
 * @description HelloController
 * @since 2021/12/27 14:02
 */
@RestController
@RequestMapping("/test")
@Slf4j
@Api(tags = "测试")
public class HelloController {

    @Resource
    private BlogTestService testService;

    @ApiOperation("测试接口")
    @GetMapping("/hello")
    public String hello(HttpServletRequest httpServletRequest) {
        log.info("HelloController.hello 路径{}", "/hello");
        Enumeration headerNames = httpServletRequest.getHeaderNames();
        //使用循环遍历请求头，并通过getHeader()方法获取一个指定名称的头字段
        while (headerNames.hasMoreElements()){
            String headerName = (String) headerNames.nextElement();
            log.error("请求头："+headerName + " : " + httpServletRequest.getHeader(headerName) + "<br/>");
        }
        return testService.hello();
    }

    @ApiOperation("hi")
    @GetMapping("/hi")
    public String hi() {
        return testService.hi();
    }

    @ApiOperation("exceptionHandler")
    @GetMapping("/exceptionHandler/{num}")
    public Integer testExceptionHandler(@PathVariable("num") Integer num) {
        //除0错误
        return 10 / num;
    }

}
